package com.splic.web.controller.product;

import java.util.List;
import com.splic.common.utils.StringUtils;
import com.splic.product.domain.Product;
import com.splic.product.domain.UserThirdBind;
import com.splic.product.dto.UserBackUpDto;
import com.splic.product.service.ProductService;
import com.splic.product.service.ProductUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.splic.common.annotation.Log;
import com.splic.common.enums.BusinessType;
import com.splic.product.domain.User;
import com.splic.product.service.UserService;
import com.splic.common.core.controller.BaseController;
import com.splic.common.core.domain.AjaxResult;
import com.splic.common.utils.poi.ExcelUtil;
import com.splic.common.core.page.TableDataInfo;

/**
 * 用户信息Controller
 * 
 * @author singe
 * @date 2022-01-04
 */
@Controller
@RequestMapping("/product/user")
public class UserController extends BaseController
{
    private String prefix = "product/user";

    @Autowired
    private UserService userServiceImpl;

    @Autowired
    private ProductUserService productUserService;

    @Autowired
    private ProductService productServiceImpl;

    @RequiresPermissions("product:user:view")
    @GetMapping()
    public String user()
    {
        return prefix + "/list";
    }

    /**
     * 查询用户信息列表
     */
    @RequiresPermissions("product:user:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(User user)
    {
        startPage();
        List<User> list = userServiceImpl.selectUserList(user);
        if (list != null && list.size() > 0) {
            productUserService.selectUserProductNumber(list);
        }
        return getDataTable(list);
    }

    /**
     * 新增用户信息
     */
    @GetMapping(value = { "/form/{id}", "/form" })
    public String form(@PathVariable(value = "id", required = false) Long id, ModelMap mmap)
    {
        if (StringUtils.isNotNull(id)){
            mmap.put("user", userServiceImpl.selectUserById(id));
        }else{
            mmap.put("user", new User());
        }
        return prefix + "/form";
    }

    /**
     * 新增保存用户信息
     */
    @RequiresPermissions("product:user:edit")
    @Log(title = "用户信息", businessType = BusinessType.INSERT)
    @PostMapping("/saveOrUpdate")
    @ResponseBody
    public AjaxResult saveOrUpdate(User user)
    {
        return toAjax(userServiceImpl.saveOrUpdate(user));
    }


    /**
     * 查看用户信息
     */
    @GetMapping(value = { "/viewThirdBind/{id}"})
    public String viewThirdBind(@PathVariable(value = "id", required = false) Long id, ModelMap mmap)
    {
        mmap.put("userId",id);
        return prefix + "/thirdBind/list";
    }

    /**
     * 查看用户信息
     */
    @GetMapping(value = { "/logout/viewThirdBind/{id}"})
    public String logoutViewThirdBind(@PathVariable(value = "id", required = false) Long id, ModelMap mmap)
    {
        mmap.put("userId",id);
        return prefix + "/thirdBind/logoutlist";
    }

    /**
     * 删除
     */
    @RequiresPermissions("product:user:remove")
    @Log(title = "用户信息", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(Long id)
    {
        return toAjax(userServiceImpl.deleteById(id));
    }


    /**
     * 导出用户信息列表
     */
    @RequiresPermissions("product:user:export")
    @Log(title = "用户信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(User user)
    {
        List<User> list = userServiceImpl.selectUserList(user);
        ExcelUtil<User> util = new ExcelUtil<User>(User.class);
        return util.exportExcel(list, "用户信息数据");
    }

    @RequiresPermissions("product:user:view")
    @GetMapping("/logoutUser")
    public String logoutUser()
    {
        return prefix + "/logoutList";
    }


    /**
     * 查询用户信息列表
     */
    @RequiresPermissions("product:user:list")
    @PostMapping("/logoutList")
    @ResponseBody
    public TableDataInfo logOutList(User user)
    {
        startPageNoOrderBy();
        List<UserBackUpDto> list = userServiceImpl.selectLogOutPageList(user);
        return getDataTable(list);
    }

    /**
     * 已安装应用名称列表
     */
    @GetMapping(value = { "/productList/{id}"})
    public String productList(@PathVariable(value = "id", required = false) Long id, ModelMap mmap)
    {
        mmap.put("id", id);
        return prefix + "/namelist";
    }

    /**
     * 已安装应用名称列表
     */
    @PostMapping(value = { "/getProductList"})
    @ResponseBody
    public TableDataInfo getProductList(Long id)
    {
        List<Product> nameList = productServiceImpl.selectProductByUserId(id);
        return getDataTable(nameList);
    }
}
